<?php
/**
 * Created by PhpStorm.
 * User: wangchaozhen
 * Date: 2018/5/30
 * Time: 22:13
 */

namespace App\Service\Goods;

use App\Models\Goods\GoodsModel;
use Illuminate\Support\Facades\DB;

class GoodsService
{
    /**
     * 获得商品列表
     */
    public static function getGoodsList($where = ['is_up'=>1], $order = 1,$offset = 0,$page_size = 15,$is_high = 1)
    {
        $goods_model = GoodsModel::where($where)
            ->select('id','name','orgin_price','current_price','thumb','sale_num')
            ->orderBy('is_recommend','desc');
        if($is_high){
            $order_desc = 'desc';
        }else{
            $order_desc = 'asc';
        }
        switch ($order){
            case 1:
                $goods_model = $goods_model->orderBy('created',$order_desc);
                break;
            case 2:
                $goods_model = $goods_model->orderBy('sale_num',$order_desc)->orderBy('created',$order_desc);
                break;
            case 3:
                $goods_model = $goods_model->orderBy('current_price',$order_desc)->orderBy('sale_num',$order_desc);
                break;
        }

        $goods_list = $goods_model->offset($offset)->limit($page_size)->get();
        return $goods_list;
    }

    /**
     * 获得商品标签
     */
    public static function getGoodsLabels()
    {
        $goods_label = DB::table('lsj_label')->get();
        return $goods_label;
    }

    /**
     * 获得商品详情
     */
    public static function getGoodsIdInfo($id)
    {
        $goods_list = GoodsModel::where('id',$id)->first();
        return $goods_list;
    }

    /**
     * 减少库存
     */
    public static function reduceStock($goods_id,$number)
    {
        $result = GoodsModel::where('id',$goods_id)->decrement('stock', $number);
        return $result;
    }

    /**
     * 添加商品销售量
     */
    public static function addGoodsSale($goods_id,$number)
    {
        $result = GoodsModel::where('id',$goods_id)->increment('sale_num',$number);
        return $result;
    }

    /**
     *  首页新品推荐
     */
    public static function newGoodsRecmd()
    {
        $result = GoodsModel::where('is_new',1)->where('is_up',1)->limit(2)->select('id','thumb','name')->orderBy('sale_num','desc')->get();

        if($result->isEmpty()){
            $result = GoodsModel::where('is_up',1)->orderBy('id','desc')->orderBy('sale_num','desc')->limit(2)->select('id','thumb','name')->get();
        }

        return $result;
    }
}